package me.firstapp.api.bbs;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.Collection;
import me.firstapp.service.bbs.CollectionService;

@Controller
@RequestMapping(value = "/collection")
@Api(value = "/collection", description = "收藏相关接口")
public class CollectionApi extends BaseApi {

	@Autowired
	private CollectionService collectionService;

	@RequestMapping(value = "findCollectionCounts.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取收藏量", notes = "获取收藏量")
	public void executeFindCollectionCounts(Long memberId, Long topicId, Long productId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Long counts = collectionService.findCollectionCounts(memberId, topicId, productId, apiClientId);
			resultJsonObject.setObject(counts);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "findMemberTopicCollectionRel.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户話題收藏关系", notes = "获取用户話題收藏关系")
	public void findMemberTopicCollectionRel(Long memberId, Long topicId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Collection> result = new SingleObject<Collection>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Collection collection = collectionService.findMemberTopicCollectionRel(memberId, topicId, apiClientId);
			result.setObject(collection);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findMemberProductCollectionRel.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户商品收藏关系", notes = "获取用户商品收藏关系")
	public void findMemberProductCollectionRel(Long memberId, Long productId, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Collection collection = collectionService.findMemberProductCollectionRel(memberId, productId, apiClientId);
			resultJsonObject.setObject(collection);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "changCollectionStatus.do", method = RequestMethod.POST)
	@ApiOperation(value = "改变用户收藏状态", notes = "改变用户收藏状态")
	public void changCollectionStatus(Long memberId, Long topicId, Long productId, Integer type, Boolean isCollection,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			if (isCollection == null) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "收藏状态");
			}
			if (isCollection) {
				collectionService.executeCancelCollection(memberId, topicId, productId, type, apiClientId);
				resultJsonObject.setObject(false);
			} else {
				collectionService.addCollection(memberId, topicId, productId, type, apiClientId);
				resultJsonObject.setObject(true);
			}
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "findMemberCollections.do", method = RequestMethod.GET)
	@ApiOperation(value = "分页查询用户收藏", notes = "分页查询用户收藏")
	public void findMemberCollections(
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long memberId,
			Integer type, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Collection> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Collection> collectionPage = collectionService.findMemberCollectionsPage(pageNo, pageSize, memberId,
					type, apiClientId);
			JsonPage<Collection> jsonPage = new JsonPage<Collection>();
			jsonPage.setList(collectionPage.getEntities());
			jsonPage.setPageNo(collectionPage.getPageNo());
			jsonPage.setPageSize(collectionPage.getPageSize());
			jsonPage.setTotalCount(collectionPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
